package cn.suimg.shortlink.service.impl;

import cn.suimg.shortlink.bean.ShortLink;
import cn.suimg.shortlink.bean.ShortLinkExample;
import cn.suimg.shortlink.dao.ShortLinkMapper;
import cn.suimg.shortlink.exception.ServiceException;
import cn.suimg.shortlink.redis.CacheKeys;
import cn.suimg.shortlink.redis.CacheOperator;
import cn.suimg.shortlink.service.ShortLinkService;
import cn.suimg.shortlink.util.CommonUtil;
import cn.suimg.shortlink.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ShortLinkServiceImpl implements ShortLinkService {


    @Resource
    private ShortLinkMapper shortLinkMapper;

    @Resource
    private CacheOperator cacheOperator;

    @Override
    public String createShortLink(String originalLink, String userId, Integer expireSecond) {
        String shortLinkId = CommonUtil.shortLinkGenerator(originalLink);
        if(shortLinkMapper.selectByExample(new ShortLinkExample(){{
            createCriteria().andShortLinkEqualTo(shortLinkId);
        }}).size() !=0 ){
            throw new ServiceException("ERROR:original link has been created");
        }
        ShortLink shortLink = new ShortLink() {{
            setShortLink(shortLinkId);
            setOriginalLink(originalLink);
            setUserId(userId);
            setCreateTime(new Date());
            setUpdateTime(DateUtil.NULL_DATE);
            setExpireTime(new Date(System.currentTimeMillis() + (expireSecond * 1000)));
            setDeleted(false);
        }};
        shortLinkMapper.insertSelective(shortLink);
        cacheOperator.set(CacheKeys.keyLink(CacheKeys.SHORT_LINK_CACHE,shortLinkId),shortLink,expireSecond);
        return shortLinkId;
    }

    @Override
    public String getOriginalLink(String shortLinkID) {
        ShortLink shortLink;
        CacheKeys cacheKeys = CacheKeys.keyLink(CacheKeys.SHORT_LINK_CACHE, shortLinkID);
        if((shortLink = cacheOperator.get(cacheKeys,ShortLink.class)) == null){
            shortLink = CommonUtil.getListItem(shortLinkMapper.selectByExample(new ShortLinkExample() {{
                createCriteria().andShortLinkEqualTo(shortLinkID).andDeletedEqualTo(false);
            }}), 0);
            if(shortLink != null){
                int expireSecond = (int) (shortLink.getExpireTime().getTime() - System.currentTimeMillis()) / 1000;
                cacheOperator.set(cacheKeys,shortLink,expireSecond);
            }
        }
        return shortLink != null ? shortLink.getOriginalLink() : null;
    }

    @Override
    public List<ShortLink> queryShortLinkList(String userId) {
        return shortLinkMapper.selectByExample(new ShortLinkExample() {{
            createCriteria().andUserIdEqualTo(userId);
        }});
    }

    @Override
    public boolean enableOrDisableLink(String userId, Integer id) {
        ShortLink shortLink = shortLinkMapper.selectByPrimaryKey(id);
        if(shortLink == null)
            throw new ServiceException("ERROR:link id not exist");
        if(!shortLink.getUserId().equals(userId))
            throw new ServiceException("ERROR:access denied");
        if(shortLink.getDeleted()){
            shortLinkMapper.updateByPrimaryKeySelective(new ShortLink(){{
                setId(id);
                setUpdateTime(new Date());
                setDeleted(false);
            }});
            return false;
        } else {
            shortLinkMapper.updateByPrimaryKeySelective(new ShortLink(){{
                setId(id);
                setUpdateTime(new Date());
                setDeleted(true);
            }});
            cacheOperator.del(CacheKeys.keyLink(CacheKeys.SHORT_LINK_CACHE,shortLink.getShortLink()));
            return true;
        }
    }
}
